package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();

        ShoppingCart shoppingCart = new ShoppingCart();

        Long userId = BaseContext.getCurrentId();

        if (dishId != null) {
            //判断是否是第一次，是插入数据
            HashMap map = new HashMap();
            map.put("dishId", dishId);
            map.put("userId", userId);
           List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(map);
            Dish dish = dishMapper.selectById(dishId);
            if (shoppingCartList == null || shoppingCartList.size() == 0) {
                shoppingCart.setName(dish.getName());
                shoppingCart.setUserId(userId);
                shoppingCart.setDishId(dishId);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);

                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());

                shoppingCartMapper.insert(shoppingCart);
            } else {
                //不是，数量+1，金额=数量*单价
                ShoppingCart cart = shoppingCartList.get(0);
                Integer number = cart.getNumber();
                cart.setNumber(++number);
                cart.setAmount(dish.getPrice().multiply(new BigDecimal(number)));
                shoppingCartMapper.update(cart);
            }

        } else if (setmealId != null) {
            //判断是否是第一次
            HashMap map = new HashMap();
            map.put("setmealId", setmealId);
            map.put("userId", userId);
            ShoppingCart cart = shoppingCartMapper.select(map).get(0);
            Setmeal setmeal = setmealMapper.selectById(setmealId);
            // 是插入数据
            if (cart == null) {
                cart = new ShoppingCart();
            cart.setName(setmeal.getName());
            cart.setUserId(userId);
            cart.setSetmealId(setmealId);
            cart.setNumber(1);
            cart.setAmount(setmeal.getPrice());

            cart.setImage(setmeal.getImage());
            cart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(cart);
            } else {
                //不是，数量+1，金额=数量*单价
                Integer number = cart.getNumber();
                cart.setNumber(++number);
                cart.setAmount(setmeal.getPrice().multiply(new BigDecimal(number)));
                shoppingCartMapper.update(cart);
            }
        }
    }

    @Override
    public List<ShoppingCart> list() {
        HashMap hashMap = new HashMap();
        hashMap.put("userId", BaseContext.getCurrentId());
        return shoppingCartMapper.select(hashMap);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();


      if (dishId != null) {
          HashMap map = new HashMap();
          map.put("dishId", dishId);
          map.put("userId", userId);
          ShoppingCart cart = shoppingCartMapper.select(map).get(0);
          if (cart != null) {
              Integer number = cart.getNumber();
              if (number >1) {
                  cart.setNumber(--number);
                  shoppingCartMapper.update(cart);
              } else if (number == 1) {
                  shoppingCartMapper.delete(map);
              }
          }
      } else if (setmealId != null) {
          HashMap map = new HashMap();
          map.put("setmealId", setmealId);
          map.put("userId", userId);
          ShoppingCart cart = shoppingCartMapper.select(map).get(0);
          if (cart != null) {
              Integer number = cart.getNumber();
              if (number >1) {
                  cart.setNumber(--number);
                  shoppingCartMapper.update(cart);
              } else if (number == 1) {
                  shoppingCartMapper.delete(map);
              }
          }
      }
    }
}
